package leetcode.D300.T213;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-22 17:45:43 星期二
 */

class Solution {
    public int rob(int[] nums) {
        if (nums.length == 1)
            return nums[0];
        return Math.max(helper(nums, 1, nums.length-1),
                Math.max(helper(nums, 0, nums.length-1),
                        helper(nums, 1, nums.length)));
    }

    private int helper(int[] nums, int start, int end) {
        int d1 = 0, d2 = 0;
        for(int i=start; i < end; ++i) {
            int temp = Math.max(d1, d2+nums[i]);
            d2 = d1;
            d1 = temp;
        }
        return Math.max(d1, d2);
    }
}